package uk.gov.swmprobation.swm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.Window;
import android.widget.TextView;

public class DocumentView extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_document_view);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);        
        
        Intent intent = getIntent();
        int rawfile = intent.getIntExtra(Documents.RAW_FILE, R.raw.mission);
        
        TextView document = (TextView) findViewById(R.id.document);
        document.setText(Html.fromHtml(readText(rawfile)));
    }

    private String readText(int rawfile) {
		InputStream inputStream = getResources().openRawResource(rawfile);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

		int i;

		try {
			i = inputStream.read();
			while (i != -1) {
				byteArrayOutputStream.write(i);
				i = inputStream.read();
			}
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return byteArrayOutputStream.toString();
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_document_view, menu);
        return true;
    }
}
